/*******************************************************************
 *
 * @Version : 0.5 
 *
 * ListAction.java 
 *
 * 2011-10-17 下午05:34:55
 *
 *******************************************************************/
package game.breeze.ani;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.AnimationAction;
import com.badlogic.gdx.scenes.scene2d.actions.ActionResetingPool;
import com.badlogic.gdx.scenes.scene2d.actors.Image;

/**
 * Class desc there
 * 
 * @author yangwei ywengineer@gmail.com
 * 
 */
public class ListAction extends AnimationAction {
	private static final ActionResetingPool<ListAction> pool = new ActionResetingPool<ListAction>(4, 100) {
		@Override
		protected ListAction newObject() {
			return new ListAction();
		}
	};

	protected Animation ani;
	protected float update;
	protected int aniMode;

	public static ListAction $(Animation ani, float duration, int aniMode) {
		ListAction action = pool.obtain();
		action.duration = duration;
		action.ani = ani;
		action.aniMode = aniMode;
		action.invDuration = 1 / duration;
		ani.reset();
		return action;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.badlogic.gdx.scenes.scene2d.Action#setTarget(com.badlogic.gdx.scenes
	 * .scene2d.Actor)
	 */
	@Override
	public void setTarget(Actor actor) {
		this.target = actor;
		this.taken = 0;
		this.done = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.badlogic.gdx.scenes.scene2d.Action#act(float)
	 */
	@Override
	public void act(float delta) {
		update += delta;
		done = ani.isDone();
		if (done) {
			ani = null;
		} else {
			TextureRegion region = ani.getKeyFrame(update, aniMode);
			if (getTarget() instanceof Image) {
				((Image) getTarget()).region = region;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.badlogic.gdx.scenes.scene2d.AnimationAction#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		pool.free(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.badlogic.gdx.scenes.scene2d.Action#copy()
	 */
	@Override
	public Action copy() {
		ListAction moveTo = $(ani, duration, aniMode);
		if (interpolator != null)
			moveTo.setInterpolator(interpolator.copy());
		return moveTo;
	}

}
